// 普通用户修改信息
$(function () {
    $('.navbarconent #toupdate').click(function () {
        $('.updateauthorInfo').css({ "display": "block", "transition": "all 0.5s ease 0s" })
        $('.navbarconent .boxContent').css({ "opacity": 0.1, "transition": "all 1s ease 0s" })
    })
    $('.updateauthorInfo #close').click(function () {
        $('.updateauthorInfo').css({ "display": "none", "transition": "all 0.5s ease 0s" })
        $('.navbarconent .boxContent').css({ "opacity": 1, "transition": "all 1s ease 0s" })
    })

    // 上传图片并预览
    var inputpic = document.getElementById("inputfile")
    inputpic.onchange = function () {
        var file = this.files[0]
        var imageUrl = URL.createObjectURL(file);
        document.getElementById("willsetImg").src = imageUrl;
        document.getElementById("willsetImg").style = "width:200px,height:200px";
    }

    let mottoResult = document.getElementById("authormotto").value
    let arr = [mottoResult.split(",")][0]
    let motto = document.getElementsByTagName("textarea")[0]
    motto.innerText = arr


    // 修改信息----发送邮件
    $("#send").click(e => {
        e.preventDefault()
        var email = $('#email')
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/g;//邮箱验证

        var countDownTime = parseInt(60);
        function countDown(countDownTime) {
            var timer = setInterval(function () {
                if (countDownTime >= 0) {
                    $("#send").val(countDownTime + 's');
                    $("#send").prop('disabled', true)
                    $("#send").attr("class", "send btn-success col-md-3 col-sm-3 col-xs-3")
                    countDownTime--;
                } else {
                    clearInterval(timer);
                    $("#send").val('sent');
                    $("#send").prop('disabled', false)
                }
            }, 1000);
        }
        if (reg.test(email.val())) {
            email.css("border-color", "green");
            $("#send").css("background-color", "#5cb85c")
            countDown(countDownTime);
            $.ajax({
                url: '/sendemail',
                type: 'post',
                data: { email: email.val(), author: $('#authorname').val() },
                dataType: 'json',
                success: (d) => {
                    console.log("success");
                },
                error: () => {
                    swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                        window.location.href = "/login"
                    })
                }
            })
        } else {
            email.css("border-color", "red");
            $("#send").css("background-color", "#c9302c")
        }
    })
    // 修改用户信息
    $("form").submit(e => {
        e.preventDefault()
        var formData = new FormData($("form")[0])
        $.ajax({
            url: '/updateauthorinfo',
            type: 'post',
            data: formData,
            cache: false,
            contentType: false, //已经在表单的entype处声明了multipart/form-data
            processData: false, //data值是FormData对象，不需要对数据做处理
            success: (d) => {
                console.log("success");
                switch (d.code) {
                    case 1:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 2:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 3:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 4:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 5:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 6:
                        swal.fire({ title: d.msg, type: "success", confirmButtonText: '请重新登陆' }).then(() => {
                            window.location.href = "/login"
                        })
                        break;
                }
            },
            error: () => {
                swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                    window.location.href = "/login"
                })
            }
        })
    })
})